#include "app.h"

void fwdgt_init(void)
{
	rcu_periph_clock_enable(RCU_WWDGT);
	fwdgt_config(4095, FWDGT_PSC_DIV4);
	fwdgt_enable();
}

void user_timer_init(void)
{
	timer_parameter_struct tm5;
	
	rcu_periph_clock_enable(RCU_TIMER5);
	
	tm5.clockdivision = TIMER_CKDIV_DIV1;  //72Mhz
	tm5.prescaler = 7200-1; //10000hz
	tm5.period = 1-1;       //5ms
	tm5.counterdirection = TIMER_COUNTER_UP;
	tm5.alignedmode = TIMER_COUNTER_EDGE;
	tm5.repetitioncounter = 0;
	timer_deinit(TIMER5);
	
	timer_init(TIMER5, &tm5);
	timer_autoreload_value_config(TIMER5, 5-1);
	timer_auto_reload_shadow_enable(TIMER5);
	
	timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
	timer_interrupt_enable(TIMER5, TIMER_INT_UP);
	
	NVIC_SetPriority(TIMER5_IRQn, 11);
	NVIC_EnableIRQ(TIMER5_IRQn);
	
	timer_enable(TIMER5);
	
}

void TIMER5_IRQHandler(void)
{
	uint8_t i = 0;
	if(SET==timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP))
	{
		timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
		for( ; i<USART_MAX_NUM; ++i )
		{
			if( serials[i]!=(void*)0 )
			{
				open_usart_send(serials[i]);
			}
		}
	}
}

